Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls


Public Partial Class User_Edit
        Inherits System.Web.UI.Page
	
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        Dim UserName As String = Request.QueryString("UserId")
        Dim UserAccount As MembershipUser = Membership.GetUser(UserName)
        Dim UserProfile As ProfileCommon = Profile.GetProfile(UserName)

        If (Not Page.IsPostBack) Then
            'Fill Membership Information
            uname.Text = UserAccount.UserName
            email.Text = UserAccount.Email
            isApproved.Checked = UserAccount.IsApproved

            'Fill Roles and Profile Information
            If Roles.IsUserInRole(UserName, "ProjectAdministrator") Then
                rolePrjAdmin.Checked = True
            End If
            If Roles.IsUserInRole(UserName, "ProjectManager") Then
                rolePrjMgr.Checked = True
            End If
            If Roles.IsUserInRole(UserName, "Consultant") Then
                roleUser.Checked = True
            End If

            'Is User Account Locked
            If UserAccount.IsLockedOut Then
                lblIsLocked.Visible = True
                btnUnlock.Visible = True
                lblUnLockTitle.Visible = True
            Else
                lblIsLocked.Visible = False
                btnUnlock.Visible = False
                lblUnLockTitle.Visible = False
            End If

            lname.Text = UserProfile.LastName
            fname.Text = UserProfile.FirstName
            empid.Text = UserProfile.EmpId


        End If


    End Sub
    
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim UserName As String = uname.Text
        Dim UserAccount As MembershipUser = Membership.GetUser(UserName)
        Dim UserProfile As ProfileCommon = Profile.GetProfile(UserName)

        'Update data
        If (rolePrjAdmin.Checked) And (Not Roles.IsUserInRole(UserName, "ProjectAdministrator")) Then
            Roles.AddUserToRole(UserName, "ProjectAdministrator")
        End If
        If (rolePrjMgr.Checked) And (Not Roles.IsUserInRole(UserName, "ProjectManager")) Then
            Roles.AddUserToRole(UserName, "ProjectManager")
        End If
        If (roleUser.Checked) And (Not Roles.IsUserInRole(UserName, "Consultant")) Then
            Roles.AddUserToRole(UserName, "Consultant")
        End If

        'Check unselected Roles
        If (Not rolePrjAdmin.Checked) And (Roles.IsUserInRole(UserName, "ProjectAdministrator")) Then
            Roles.RemoveUserFromRole(UserName, "ProjectAdministrator")
        End If
        If (Not rolePrjMgr.Checked) And (Roles.IsUserInRole(UserName, "ProjectManager")) Then
            Roles.RemoveUserFromRole(UserName, "ProjectManager")
        End If
        If (Not roleUser.Checked) And (Roles.IsUserInRole(UserName, "Consultant")) Then
            Roles.RemoveUserFromRole(UserName, "Consultant")
        End If

        'Save the User Account info
        UserAccount.Email = email.Text
        UserAccount.IsApproved = isApproved.Checked

        'Save the Profile info
        UserProfile.LastName = lname.Text
        UserProfile.FirstName = fname.Text
        UserProfile.EmpId = empid.Text
        UserProfile.Save()

        Response.Redirect("User_List.aspx")

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Response.Redirect("User_List.aspx")

    End Sub

    Protected Sub btnChgPass_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChgPass.Click

        Dim UserName As String = uname.Text
        Dim UserAccount As MembershipUser = Membership.GetUser(UserName)

        errMsg.Visible = False

        Dim tempPass As String = UserAccount.ResetPassword()
        Try
            UserAccount.ChangePassword(tempPass, txtPass.Text)
            Response.Redirect("User_List.aspx")
        Catch ex As Exception
            errMsg.Text = ex.Message
            errMsg.Visible = True
        End Try

    End Sub

    Protected Sub btnUnlock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUnlock.Click
        'Unlock the User Account
        Dim UserName As String = uname.Text
        Dim UserAccount As MembershipUser = Membership.GetUser(UserName)

        UserAccount.UnlockUser()
        btnUnlock.Visible = False
        lblIsLocked.Visible = False
        lblUnLockTitle.Visible = False
    End Sub
End Class
